package com.cyclecoder.thread;

import java.util.concurrent.Exchanger;
import java.util.concurrent.Executor;

/**
 * @author by zhanglei
 * @Date 2021/8/10
 */
public class ThreadExchanger {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();
        new Thread(() -> {
            try {
                String exchanger1 = exchanger.exchange("第一个线程"+ Thread.currentThread().getName());
                System.out.println("第一个线程得到的数据"+ exchanger1);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                String exchanger2 = exchanger.exchange("第二个线程"+ Thread.currentThread().getName());
                System.out.println("第二个线程得到的数据"+ exchanger2);
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                String exchanger3 = exchanger.exchange("第三个线程"+ Thread.currentThread().getName());
                System.out.println("第三个线程得到的数据"+ exchanger3);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                String exchanger3 = exchanger.exchange("第si个线程"+ Thread.currentThread().getName());
                System.out.println("第si个线程得到的数据"+ exchanger3);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
